﻿using CoffeeShop.BIZ;
using CoffeeShop.Common;
using CoffeeShop.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace CoffeeShop.Controllers
{
    public class UserController : Controller
    {
        private UserBiz _userBiz = new UserBiz();

        [AllowAnonymous]
        public ActionResult Login()
        {
            var isLogged = HttpContext.User.Identity.IsAuthenticated;
            if (isLogged)
            {
                return RedirectToAction("Index", "Home");
            }
            ViewBag.NotFoundUser = false;
            return View();
        }

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login(User model)
        {
            string passwordMD5 = CmnFunc.ConvertToMD5(model.Password);
            
            if (Membership.ValidateUser(model.UserName, passwordMD5))
            {
                FormsAuthentication.SetAuthCookie(model.UserName, false);
                Session["UserName"] = model.UserName;
                return RedirectToAction("Index", "Home");
            }
            ViewBag.NotFoundUser = true;
            return View();
        }

        [AllowAnonymous]
        public ActionResult Register()
        {
            // Variable to detech duplicate UserName
            ViewBag.IsDuplicateUserName = false;
            User user = new User();
            user.DateOfBirth = DateTime.Today;
            return View(user);
        }

        [HttpPost]
        [AllowAnonymous]
        public ActionResult Register(User model)
        {
            try
            {
                if (_userBiz.IsDuplicateUserName(model.UserName))
                {
                    ViewBag.IsDuplicateUserName = true;
                    return View(model);
                }
                string dateOfBirth = Request.Form["DateOfBirth"];
                string gender = Request.Form["Gender"];
                model.RoleId = CmnFunc.GetRoleByRoleName(CmnFunc.CUSTOMER_ROLE_VALUE);
                model.DateOfBirth = DateTime.ParseExact(dateOfBirth, "dd/MM/yyyy", null);
                model.Password = CmnFunc.ConvertToMD5(model.Password);
                var result = _userBiz.AddNewUser(model);
                if (result == true)
                {
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ViewBag.IsDuplicateUserName = false;
                    return View(model);
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.ToString());
                throw ex;
            }
        }

        [AllowAnonymous]
        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Login");
        }

        [HttpGet]
        public JsonResult CheckDuplicateUserName(string userName)
        {
            try
            {
                var isDuplicate = _userBiz.IsDuplicateUserName(userName);

                var jsonData = new { isDuplicate };

                return Json(jsonData, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.ToString());
                throw ex;
            }
        }
    }
}
